package cn.chinamobile.controller;

import cn.chinamobile.dao.ProductDao;
import cn.chinamobile.entities.Bus;
import cn.chinamobile.entities.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
public class ProductController {
    @Autowired // 自动注入
    private ProductDao productDao;

    @RequestMapping(value = "/product/add", method = RequestMethod.POST) //url 类型
    public ResponseEntity<CommonResult<Void>> add(@RequestBody Bus body) {
        log.info("[PRODUCT] add - body: {}", body);
        try {
            int sum = productDao.addProduct(body);
            if(sum==0) {
                return new ResponseEntity<>(new CommonResult<>(400, "添加失败", null), HttpStatus.BAD_REQUEST);
            }
        } catch (Exception ex) {
            log.error("[PRODUCT] add failed - body: {}, ex: {}", body, ex.getMessage());
            return new ResponseEntity<>(new CommonResult<>(400, "添加失败", null), HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(new CommonResult<>(200, "添加成功", null), HttpStatus.OK);
    }

    @RequestMapping(value = "/product/update", method = RequestMethod.PUT)
    public ResponseEntity<CommonResult<Void>> update(@RequestBody Bus body) {
        log.info("[PRODUCT] update - body: {}", body);
        try {
            int sum = productDao.updateProduct(body);
            if(sum==0) {
                return new ResponseEntity<>(new CommonResult<>(400, "修改失败", null), HttpStatus.BAD_REQUEST);
            }
        } catch (Exception ex) {
            log.error("[PRODUCT] update failed - body: {}, ex: {}", body, ex.getMessage());
            return new ResponseEntity<>(new CommonResult<>(400, "修改失败", null), HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(new CommonResult<>(200, "修改成功", null), HttpStatus.OK);
    }

    @RequestMapping(value = "/product/delete", method = RequestMethod.DELETE)
    public ResponseEntity<CommonResult<Void>> delete(@RequestParam String id) {
        log.info("[PRODUCT] delete - id: {}", id);
        try {
            int sum = productDao.delProduct(id);
            if(sum==0) {
                return new ResponseEntity<>(new CommonResult<>(400, "删除失败", null), HttpStatus.BAD_REQUEST);
            }
        } catch (Exception ex) {
            log.error("[PRODUCT] getAll failed - ex: {}", ex.getMessage());
            return new ResponseEntity<>(new CommonResult<>(400, "删除失败", null), HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(new CommonResult<>(200, "删除成功",null), HttpStatus.OK);
    }

    @RequestMapping(value = "/product/all", method = RequestMethod.GET)
    public ResponseEntity<CommonResult<List<Bus>>> getAll() {
        List<Bus> busList = null;
        try {
            busList = productDao.getAll();
        } catch (Exception ex) {
            log.error("[PRODUCT] getAll failed - ex: {}", ex.getMessage());
            return new ResponseEntity<>(new CommonResult<>(400, "查询失败", null), HttpStatus.BAD_REQUEST);
        }
        log.info("[PRODUCT] getAll - bus: {}", busList);
        return new ResponseEntity<>(new CommonResult<>(200, "查询成功", busList), HttpStatus.OK);
    }

}
